package zjh_20230308;

import org.w3c.dom.Node;

import java.util.*;

public class MyLinkedList implements List {
    public static class Node {
        private Object value;
        private Node next;
        private Node pre;

        public Node(Node pre, Object value, Node next) {
            this.pre = pre;
            this.value = value;
            this.next = next;
        }

    }

    private int size;
    private Node first;
    private Node last;

    public MyLinkedList() {

    }

    public MyLinkedList(Node node) {
        super();
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public boolean isEmpty() {
        if (0 == size()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public boolean add(Object o) {
        if (null == o) {
            throw new IllegalArgumentException("不能存空值");
        }
        Node l = last;
        Node node = new Node(last, o, null);
        last = node;
        if (l == null) {
            first = node;
        } else {
            l.next = node;
        }
        size++;
        new MyLinkedList(node);
        return true;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean addAll(Collection c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public Object get(int index) {
        return null;
    }

    @Override
    public Object set(int index, Object element) {
        return null;
    }


    @Override
    public void add(int index, Object element) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("非法下标");
        }
        Node f = first;
        if (0 == index) {
            Node node = new Node(null, element, getNode(index).next);
            first = node;
            size++;
        }if (size == index) {
            add(element);
        } else {
            Node nodePre = getNode(index).pre;
            Node newNode = new Node(nodePre, element, nodePre.next);
            if (nodePre == null) {
                first = newNode;
            } else {
                nodePre.next = newNode;
                first = nodePre;
            }

            size++;
        }
//        final Node pred = element.pre;
//        final Node newNode = new Node(pred, element, element);
//        element.pre = newNode;
//        if (pred == null) {
//            first = newNode;
//        } else {
//            pred.next = newNode;
//        }
    }

    public Node getNode(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        if (index < (size >> 1)) {
            Node node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        } else {
            Node node = last;
            for (int i = size - 1; i > index; i--) {
                node = node.pre;
            }
            return node;
        }
    }

    @Override
    public Object remove(int index) {
        return null;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator listIterator() {
        return null;
    }

    @Override
    public ListIterator listIterator(int index) {
        return null;
    }

    @Override
    public List subList(int fromIndex, int toIndex) {
        return null;
    }

    @Override
    public boolean retainAll(Collection c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection c) {
        return false;
    }

    @Override
    public boolean containsAll(Collection c) {
        return false;
    }

    @Override
    public Object[] toArray(Object[] a) {
        return new Object[0];
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(first.value);
            sb.append(",");
            first = first.next;
        }
        return "链表长度： " + size + "," +
//                "新添加元素：" + last.value +
                "所有元素：" + sb.toString();
    }
}